<?php

class PublicAction extends Action
{
	function index()
	{
		$this->display('login');
	}
	
	public function login()
    {
        $this->display();
    }
    
	
	public function checkLogin(){
		
		$username = trim($_POST['username']);
		$password = trim($_POST['password']);
		
		if(empty($username))
		{
			$this->error('用户名不能为空!!!');
		}
		
		if(empty($password))
		{
			$this->error('密码不能为空!!!');
		}
		$map=array();
		$map["username"]=$username;
		import('ORG.Util.RBAC');
		
		$authInfo = RBAC::authenticate($map);
//		dump($authInfo);
		if(false === $authInfo)
		{
			$this->error('帐号不存在或已禁用！');
		} 
		else
		{
			if($authInfo['password'] != md5($_POST['password'])) 
			{
				$this->error('密码错误！');
			}
			
			if($authInfo['email']=='m23linzhe@gmail.com') {
                $_SESSION['administrator'] = true;
            }
            
            $_SESSION[C('USER_AUTH_KEY')] = $authInfo['id'];
			$_SESSION['email'] = $authInfo['email'];
			$_SESSION['username'] = $authInfo['username'];
			$_SESSION['lastLoginTime'] = $authInfo['last_login_time'];
			$_SESSION['login_count'] = $authInfo['login_count'];
			$_SESSION['login_account'] = $authInfo['account'];
			
			//保存登录信息
			$User = M('User');
			Load('extend');
			$ip = get_client_ip();
			$time = time();
			$data = array();
			$data['id'] = $authInfo['id'];
			$data['last_login_time'] = $time;
			$data['login_count'] = array('exp', 'login_count+1');
			$data['last_login_ip'] = $ip;
			$User->save($data);
            // 通过RBAC类中的静态方法saveAccessList缓存访问权限
            RBAC::saveAccessList();
            
            $this->assign('jumpUrl',__APP__.'/Index/index');
            $this->success('登录成功！');
		}
		
	}
	
	public function uploadImg()  
	{
		$loginUser = Session::get('loginUser');
		$data['id'] = $loginUser['id'];
		
		import("ORG.Net.UploadFile");
		$upload = new UploadFile(); // 实例化上传类
		$upload->maxSize  = 3145728 ; // 设置附件上传大小
		$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
		$upload->savePath =  __ROOT__.'./Public/uploads/'.$data['id'].'/'; // 设置附件上传目录
		
		if(!$upload->upload()) {
			echo "<script>parent.callback('','".$upload->getErrorMsg()."',false);</script>";
//			$this->assign("message",'上传失败!!!');
//            $this->assign("jumpUrl",__APP__.'Index/settings');
		}else{// 上传成功获取上传文件信息
	        
			$info = $upload->getUploadFileInfo();
			$path = __ROOT__.'/Public/uploads/'.$data['id'].'/'.$info[0]['savename'];
			$data['image']= $info[0]['savename'];
			
			$Member = M('User');
			if(!$Member->save($data)){
	        	echo "<script>parent.callback('','上传图片失败！',false);</script>";
	        }
			
			$loginUser = M('User')->find($data['id']);
			Session::set('loginUser', $loginUser);
	        echo "<script>parent.callback('".$path."','上传图片成功！',true);</script>";
//			$this->assign("message",'上传图片成功!!!');
//          $this->assign("jumpUrl",__APP__.'Index/settings');
		}
//	    echo "<script>callback('pp',true)</script>";
//	    echo "<script>alert('sss')</script>";
	}
	
	function logout(){
		if(isset($_SESSION[C('USER_AUTH_KEY')])) {
			unset($_SESSION[C('USER_AUTH_KEY')]);
			unset($_SESSION);
			session_destroy();
            $this->assign("jumpUrl",__URL__.'/login/');
            $this->success('登出成功！');
        }else {
            $this->error('已经登出！');
        }
	}

}
?>